package com.btpj.lib_base.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.SizeUtils
import com.btpj.lib_base.R

@SuppressLint("Recycle")
class LoadingLayout constructor(context: Context, attrs: AttributeSet, def: Int) :
    FrameLayout(context, attrs, def) {
    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

     val Success: Int = 0//成功

     val Empty: Int = 1//没有数据

     val Error: Int = 2//失败

     val Loading: Int = 3//正在请求中

    init {
        context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout).apply {
            isFirstVisible = getBoolean(R.styleable.LoadingLayout_isFirstVisible, false)
            pageBackground = getColor(R.styleable.LoadingLayout_pageBackground,
                ContextCompat.getColor(context, R.color._ffffff))
            recycle()
        }
    }

    var isFirstVisible: Boolean = false
    var pageBackground: Int = 0


    lateinit var contentView: View
    var loadingPage: View? = null

    lateinit var errorPage: View
    lateinit var emptyPage: View

    lateinit var errorImg: ImageView
    lateinit var emptyImg: ImageView
    lateinit var errorText: TextView
    lateinit var emptyText: TextView

    lateinit var errorBtn: TextView
    lateinit var emptyBtn: TextView

    var status: Int = Loading
        set(value) {
            when (value) {
                Success -> {
                    contentView.visibility = VISIBLE
                    emptyPage.visibility = GONE
                    errorPage.visibility = GONE
                    loadingPage!!.visibility = GONE
                    field = value
                }
                Loading -> {
                    contentView.visibility = GONE
                    errorPage.visibility = GONE
                    emptyPage.visibility = GONE
                    loadingPage!!.visibility = VISIBLE
                }
                Empty -> {
                    contentView.visibility = GONE
                    errorPage.visibility = GONE
                    emptyPage.visibility = VISIBLE
                    loadingPage!!.visibility = GONE
                }
                Error -> {
                    contentView.visibility = GONE
                    errorPage.visibility = VISIBLE
                    emptyPage.visibility = GONE
                    loadingPage!!.visibility = GONE
                }
            }
        }

    var onClick: onReloadListener? = null

    override fun onFinishInflate() {
        super.onFinishInflate()
        if (childCount > 1) {
            throw IllegalStateException("LoadingLayout can host only one direct child")
        }
        contentView = this.getChildAt(0)
        if (!isFirstVisible) {
            contentView.visibility = GONE
        }
        build()
    }

    fun build() {
        if (loadingPage == null) {
            loadingPage = LayoutInflater.from(context).inflate(R.layout.widget_loading_page, null)
        }
        errorPage = LayoutInflater.from(context).inflate(R.layout.widget_error_page, null)
        emptyPage = LayoutInflater.from(context).inflate(R.layout.widget_empty_page, null)
        errorText = errorPage.findViewById(R.id.empty_text)
        errorImg = errorPage.findViewById(R.id.empty_img)
        emptyImg = emptyPage.findViewById(R.id.empty_img)
        emptyText = emptyPage.findViewById(R.id.empty_text)
        errorBtn = errorPage.findViewById(R.id.error_reload_btn)
        emptyBtn = emptyPage.findViewById(R.id.error_reload_btn)
        errorBtn.setOnClickListener {
            onClick?.onReload(this)
        }

        emptyBtn.setOnClickListener {
            onClick?.onReload(this)
        }

        this.addView(emptyPage)
        this.addView(errorPage)
        this.addView(loadingPage)
    }


    public fun setEmptyText(text: String) {
        emptyText.text = text
    }

    public fun setErrorText(text: String) {
        errorText.text = text
    }

    public fun setEmptyImage(@DrawableRes id: Int) {
        emptyImg.setImageResource(id)
    }

    public fun setErrorImage(@DrawableRes id: Int) {
        errorImg.setImageResource(id)
    }


    public fun errorTextSize(sp: Float) {
        errorText.textSize = SizeUtils.dp2px(sp).toFloat()
    }

    public fun emptyTextSize(sp: Float) {
        emptyText.textSize = SizeUtils.dp2px(sp).toFloat()
    }

    public fun setEmptyImageVisible(bool: Boolean) {
        if (bool) {
            emptyImg.visibility = VISIBLE
        } else {
            emptyImg.visibility = GONE
        }
    }

    public fun setErrorVisible(bool: Boolean) {
        if (bool) {
            errorImg.visibility = VISIBLE
        } else {
            errorImg.visibility = GONE
        }
    }

    public fun setErrorBtnVisible(bool: Boolean) {
        if (bool) {
            errorBtn.visibility = VISIBLE
        } else {
            errorBtn.visibility = GONE
        }
    }

    public fun setEmptyBtnVisible(bool: Boolean) {
        if (bool) {
            emptyBtn.visibility = VISIBLE
        } else {
            emptyBtn.visibility = GONE
        }
    }

    fun setEmptyBtnText(text: String) {
        emptyBtn.text = text
    }

    fun setErrorBenText(text: String) {
        errorBtn.text = text
    }

    fun setEmptyBtnTextColor(@ColorInt id: Int) {
        emptyBtn.setTextColor(id)
    }

    fun setErrorBtnTextColor(@ColorInt id: Int) {
        errorBtn.setTextColor(id)
    }

    interface onReloadListener {
        fun onReload(v: View)
    }

    public fun setReloadListener(click: onReloadListener) {
        onClick = click
    }
}